import * as FileApi from '@/api/download';

export const getLocalData = async () => {
  if (localStorage.getItem('localData')) {
    return Promise.resolve(JSON.parse(localStorage.getItem('localData') as any));
  } else {
    const res = await FileApi.getJSON({});
    if (res) {
      localStorage.setItem('localData', JSON.stringify(res));
      return Promise.resolve(res);
    }
    return Promise.resolve(null);
  }
}
export const getFu = async () => {
  const localData = await getLocalData();
  const _data = localData['釜'];
  const data: any = [];
  _data.forEach((item, index) => {
    const obj = {
      "deviceId": index,
      "deviceName": item['设备名称'],
      "deviceStatus": item['状态'],
      "color": item['颜色'],
      "productionPercentage": 70,
      "targetedValues": [{
        "dataTypeId": ``,
        "dataTypeName": "重量",
        "dateTypeCode": "weight",
        "unitId": '',
        "unitName": item['数据']['重量']['单位'],
        "value": item['数据']['重量']['值']
      }, {
        "dataTypeId": ``,
        "dataTypeName": "压强",
        "dateTypeCode": "pressure",
        "unitId": '',
        "unitName": item['数据']['压强']['单位'],
        "value": item['数据']['压强']['值']
      }, {
        "dataTypeId": ``,
        "dataTypeName": "温度",
        "dateTypeCode": "temp",
        "unitId": '',
        "unitName": item['数据']['温度']['单位'],
        "value": item['数据']['温度']['值']
      }, {
        "dataTypeId": ``,
        "dataTypeName": "频率",
        "dateTypeCode": "frequency",
        "unitId": '',
        "unitName": item['数据']['频率']['单位'],
        "value": item['数据']['频率']['值']
      }],
    }
    data.push(obj);
  });
  return data;
}
export const getDateMonthRateCount = async () => {
  const localData = await getLocalData();
  const _data = localData['稼动率'];
  return {
    xaxisList: _data['日期'],
    yaxisList: [{
      "name": "稼动率",
      "data": _data['数据'],
      "type": "bar"
    }]
  }
}

export const getDateMonthCount = async () => {
  const localData = await getLocalData();
  const _data = localData['产量'];
  return {
    xaxisList: _data['日期'],
    yaxisList: [{
      "name": "产量",
      "data": _data['数据'],
      "type": "bar"
    }]
  }
}
export const getDateHisCount = async () => {
  const localData = await getLocalData();
  const _data = localData['出货量'];
  return {
    xaxisList: _data['日期'],
    yaxisList: [{
      "name": "出货量",
      "data": _data['数据'],
      "type": "bar"
    }]
  }
}
export const getCurrentValue = async () => {
  const localData = await getLocalData();
  const _data = localData['环保检测'];
  return [
    {
      "name": "环境检测仪01",
      "value": _data['非甲烷总烃'],
      "unit": "通用",
      "classifyName": "非甲烷总烃"
    },
    {
      "name": "环境检测仪02",
      "value": _data['氨氮用量'],
      "unit": "通用",
      "classifyName": "氨氮用量"
    },
    {
      "name": "环境检测仪02",
      "value": _data['化学需氧量'],
      "unit": "通用",
      "classifyName": "化学需氧量"
    },
    {
      "name": "环境检测仪02",
      "value": _data['PH值'],
      "unit": "通用",
      "classifyName": "PH值"
    }
  ]
}

export const getEnergy = async () => {
  const localData = await getLocalData();
  const _data = localData['能耗'];
  return {
    "water": {
      "changeRate": _data['水']['环比上月'],
      "total": _data['水']['本月'],
      "lastTotal": _data['水']['上月'],
      "lastMonth": _data['水']['上月数据'],
      "curMonth": _data['水']['本月数据']
    },
    "electricity": {
      "changeRate": _data['电']['环比上月'],
      "total": _data['电']['本月'],
      "lastTotal": _data['电']['上月'],
      "lastMonth": _data['电']['上月数据'],
      "curMonth": _data['电']['本月数据']
    },
    "gas": {
      "changeRate": _data['燃气']['环比上月'],
      "total": _data['燃气']['本月'],
      "lastTotal": _data['燃气']['上月'],
      "lastMonth": _data['燃气']['上月数据'],
      "curMonth": _data['燃气']['本月数据']
    }
  }
}

export const getTotal = async () => {
  const localData = await getLocalData();
  const _data = localData['库存'];
  return {
    "finishedInventory": _data['成品库存'],
    "materialInventory": _data['原材库存'],
    "total": _data['库存汇总']
  }
}

